<?php

class NgController extends Controller {

    public function actionIndex() {
        $this->render('index');
    }

    public function actionList() {
        $criteria = new CDbCriteria();
        $criteria->order = 't.id DESC';
        //$criteria->compare('id', "< 100");
        $criteria->limit = 500;
        $ng = Ng::model()->findAll($criteria);
        $result = array();
        if (!empty($ng)) {
            foreach ($ng as $val) {
                array_push($result, $val->attributes);
            }
        }
        echo json_encode($result);
    }
    
    public function actionHui() {
        $criteria = new CDbCriteria();
        $criteria->order = 't.id DESC';
        $criteria->compare('id', "< 100");
        $ng = Ng::model()->findAll($criteria);
        $result = array();
        if (!empty($ng)) {
            foreach ($ng as $val) {
                array_push($result, $val->attributes);
            }
        }
        echo json_encode($result);
    }

    public function actionAdd() {
        $model = new Ng;
        $model->username = Yii::app()->request->getParam('username');
        $model->email    = Yii::app()->request->getParam('email');
        $model->password = Yii::app()->request->getParam('password');
        if ($model->save()) {
            echo json_encode(array('msg' => 'ok'));
        } else {
            echo json_encode(array('msg' => 'no'));
        }
    }
    
    public function actionDelete() {
        $id = Yii::app()->request->getParam('id');
        $model = Ng::model()->deleteByPk($id);
        if($model){
            echo json_encode(array('msg' => 'ok'));
        }else{
            echo json_encode(array('msg' => 'no'));
        }
    }
    
    public function actionEdit() {
        $id       = Yii::app()->request->getParam('id');
        $username = Yii::app()->request->getParam('username');
        $email    = Yii::app()->request->getParam('email');
        $password = Yii::app()->request->getParam('password');
        
        $model = Ng::model()->findByPk($id);
        $model->username = $username;
        $model->email    = $email;
        $model->password = $password;
        if($model->save()){
             echo json_encode(array('msg' => 'ok'));
        }else{
             echo json_encode(array('msg' => 'no'));
        }
    }
    
    public function actionTest(){
        $num = 10;
        
        for($i=0; $i<$num; $i++){
             $model = new Ng();
             $model->username = uniqid();
             $model->email    = 'test@shareweb.com';
             $model->password = uniqid();
             $model->save();
             echo 'Loaded: '.$i.'<br>';
        }
    }
    
    

}
